/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package com.buzzwords.behaviors;

import com.buzzwords.model.Buzzword;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import javax.jdo.Query;

/**
 *
 * @author potlow
 */
public class BuzzwordWordBehavior extends BuzzwordBehavior {

    @Override
    public List<Object> read(Object value, List<String> properties) {
        String word;
        if (value instanceof String) {
            word = (String) value;
            if (word.isEmpty()) {
                throw new IllegalArgumentException("Search string cannot be empty");
            }
        } else {
            throw new IllegalArgumentException("Search value must be a String");
        }

        Query query = pm.newQuery(Buzzword.class);
        query.setFilter("word == wordParam");
        query.declareParameters("String wordParam");

        // get results
        try {
            List<Object> objects = (List<Object>) query.execute(word);
            List<Object> objectsToDetach = new ArrayList<Object>(objects);
            for (Object object : objects) {
                Buzzword buzzword = (Buzzword) object;

                if (properties.contains(Buzzword.BUZZWORD_LOCALES)) {
                    buzzword.getLocales();
                }
            }
            pm.detachCopyAll(objectsToDetach);
            return objects;
        } finally {
            query.closeAll();
        }
    }

    @Override
    public List<Object> excute(String filter, String declareParameters, Map<String, Object> parameters, List<String> properties) {
        throw new UnsupportedOperationException("Not supported yet.");
    }

}
